﻿#include "decodeVideo.h"
#include "ioQueue.h"

#include <QDebug>

#include <crtdbg.h>
#include <assert.h>

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

decodeVideo::decodeVideo(QObject *parent)
    : decodeBase(parent)
{
}

void decodeVideo::run()
{
    AVFrame *pFrame = av_frame_alloc();

    int frameFinished/*, pts*/;
    d->isExit = false;
    while (!d->isExit) {
        if (NULL == (d->pkt = d->queue->popup(d->streamIndex))){
            QThread::msleep(10);
            continue;
        }
        QThread::msleep(30);
        avcodec_decode_video2(d->cc, pFrame, &frameFinished, d->pkt);
        static int count = 0;
        if(++count % 100 == 0)
            qDebug()<<count;
        if (frameFinished) {
            //qDebug()<<"succeed";
        }
        //else {
        //    qDebug()<<"fail";
        //}
        av_free_packet(d->pkt);
        av_freep(d->pkt);
    }
    qDebug()<<"decodeVideo exit";
}

decodeVideo::~decodeVideo()
{
    //父类释放就可以了
    //delete d;
}
